package org.zfm.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zfm.base.BaseDomain;
import org.zfm.org.domain.Department;
import org.zfm.org.query.DepartmentQuery;
import org.zfm.org.service.IDepartmentService;
import org.zfm.util.AjaxResult;
import org.zfm.util.PageData;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/department")
@Api(tags = "部门管理")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @PutMapping
    @ApiOperation(value = "保存（新增/修改）")
    public AjaxResult save(@RequestBody Department department){
        try {
            if(department.getId() != null){
                department.setUpdate_time(new Date());
                departmentService.update(department);
            }else {
                department.setCreate_time(new Date());
                departmentService.insert(department);
            }
            return AjaxResult.getBean();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setMessage("保存失败"+e.getMessage()).setSuccess(false);
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.getBean();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查询")
    public AjaxResult pageList(@RequestBody DepartmentQuery query){
        try {
            PageData<Department> departmentPageData = departmentService.queryPageData(query);
            return AjaxResult.getBean().setResult(departmentPageData);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage("查询失败！"+e.getMessage());
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            Integer rows = departmentService.batchDelete(ids);
            return AjaxResult.getBean().setResult(rows);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @GetMapping
    @ApiOperation(value = "查询全部")
    public AjaxResult selectAll(){
        try {
            List<Department> departments = departmentService.selectAll();
            return AjaxResult.getBean().setResult(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage("查询失败！"+e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    public AjaxResult getOne(@PathVariable("id")Long id){
        try {
            Department department = departmentService.selectById(id);
            return AjaxResult.getBean().setResult(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage("查询失败！"+e.getMessage());
        }
    }

    @PostMapping("/tree")
    public AjaxResult getTree(@RequestBody BaseDomain department){
        try {
           List<Department> tree = departmentService.getTree(department);
           return AjaxResult.getBean().setResult(tree);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getBean().setSuccess(false).setMessage("查询失败！"+e.getMessage());
        }
    }
}
